package CommonGui;


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;

import jpos.JposException;
import jpos.POSPrinter;
import jpos.POSPrinterConst;

public class PrinterTry extends JFrame {
	private static final long serialVersionUID = 1L;
	private JPanel jPanel_Frame;
	private JPanel jPanel_ReceiptRetail;
	private JPanel jPanel_ReceiptBanking;
	private JPanel jPanel_CutMode;
	private JButton jButton_SamplePrint;
	private Insets Insets_Button;
	private POSPrinter jPos_Printer;
	private int nCut;

	final int PRN_DI_OUTPUT_NORMAL = 100;

	public static void main(String args[]) {
		(new PrinterTry()).setVisible(true);
	}
	public Dimension getcenter() {
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize = this.getSize();
		Dimension dim = new Dimension((screenSize.width - frameSize.width) / 2,
				(screenSize.height - frameSize.height) / 2);
		return dim;
	}

	private void exitForm(WindowEvent evt) {
		System.exit(0);
	}

	private String sPace(int iLenth) {
		// TODO Auto-generated method stub
		String sSpace;
		char chSpace[] = new char[iLenth];
		for (int j = 0; j < iLenth; j++) {
			chSpace[j] = (char) 0x20;
		}
		sSpace = new String(chSpace);
		return sSpace;
	}

	private String sLine(int iLenth) {
		String sSpace;
		char chSpace[] = new char[iLenth];
		for (int j = 0; j < iLenth; j++) {
			chSpace[j] = (char) 0x2D;
		}
		sSpace = new String(chSpace);
		return sSpace;
	}
	private void InitFram() {
		jPanel_Frame = new JPanel();
		jPanel_ReceiptRetail = new JPanel();
		jPanel_ReceiptBanking = new JPanel();
		jPanel_CutMode = new JPanel();
		jButton_SamplePrint = new JButton();
		Insets_Button = new Insets(5, 5, 5, 5);
		jPos_Printer = new POSPrinter();
		
		setSize(360, 350);
		setLocation(this.getcenter().width, this.getcenter().height);
		getContentPane().add(jPanel_Frame, "Center");
		this.setResizable(false);
		this.setTitle("Sample [Printer]");
		
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent evt) {
				exitForm(evt);
			}
		});
		
		jPanel_Frame.setLayout(new BoxLayout(jPanel_Frame, 0));
		jPanel_Frame.setMinimumSize(new Dimension(320, 335));
		jPanel_Frame.setPreferredSize(new Dimension(320, 335));
		
		jButton_SamplePrint.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				String ESC = ((char) 0x1b) + "";
				String sBuf = "";

				try {
					jPos_Printer.getCapRecPresent();
				} catch (JposException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null,
							"This Printer doesn't have the Receipt Station.",
							"Printer", 0);
					return;
				}

				try {
					jPos_Printer.transactionPrint(
							POSPrinterConst.PTR_S_RECEIPT,
							POSPrinterConst.PTR_TP_TRANSACTION);
				} catch (JposException e1) {
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null,
							"Can't use a POS Printer.", "Printer", 0);
					return;
				}

				try {
					jPos_Printer.printNormal(POSPrinterConst.PTR_S_RECEIPT, ESC
							+ "|cA" + ESC + "|2C" + "BTP-R880NP\n");
					jPos_Printer.printNormal(POSPrinterConst.PTR_S_RECEIPT, ESC
							+ "|500uF");// if MapMode is set to
					// PTR_MM_METRIC,then Feed the paper 5
					// millimeter (500*0.01 millimeter = 5
					// millimeter)
					jPos_Printer.printNormal(POSPrinterConst.PTR_S_RECEIPT, ESC
							+ "|cA" + ESC + "|2C" + "POS Thermal Printer\n");// Aligns
					// the
					// text
					// in
					// the
					// center
					// and
					// Prints
					// text
					// in
					// double-wide
					// characters.
					jPos_Printer.printNormal(POSPrinterConst.PTR_S_RECEIPT, ESC
							+ "|800uF");// if MapMode is set to
					// PTR_MM_METRIC,then Feed the paper 8
					// millimeter (800*0.01 millimeter = 8
					// millimeter)
					jPos_Printer.printNormal(POSPrinterConst.PTR_S_RECEIPT, ESC
							+ "|cA" + "Easy to load\n");// Aligns the text in
					// the center.
					jPos_Printer.printNormal(POSPrinterConst.PTR_S_RECEIPT, ESC
							+ "|cA" + "Easy to use\n");// Aligns the text in the
					// center.
					jPos_Printer.printNormal(POSPrinterConst.PTR_S_RECEIPT, ESC
							+ "|800uF");// if MapMode is set to
					// PTR_MM_METRIC,then Feed the paper 8
					// millimeter (800*0.01 millimeter = 8
					// millimeter)
					jPos_Printer.printNormal(POSPrinterConst.PTR_S_RECEIPT, ESC
							+ "|rA" + "The Leader for Printer Solution\n");// Aligns
					// the
					// text
					// at
					// the
					// right.
					jPos_Printer.printNormal(POSPrinterConst.PTR_S_RECEIPT, ESC
							+ "|500uF");// if MapMode is set to
					// PTR_MM_METRIC,then Feed the paper 5
					// millimeter (500*0.01 millimeter = 5
					// millimeter)
					jPos_Printer.printNormal(POSPrinterConst.PTR_S_RECEIPT, ESC
							+ "|cA" + ESC + "|2vC" + "Double High\n");// Aligns
					// the
					// text
					// in
					// the
					// center
					// and
					// prints
					// with
					// the
					// height
					// scaled
					// 2
					// times
					// the
					// normal
					// size
					jPos_Printer.printNormal(POSPrinterConst.PTR_S_RECEIPT, ESC
							+ "|cA" + ESC + "|2hC" + "Double Wide\n");// Aligns
					// the
					// text
					// in
					// the
					// center
					// and
					// prints
					// with
					// the
					// width
					// scaled
					// 2
					// times
					// the
					// normal
					// size,
					jPos_Printer.printNormal(POSPrinterConst.PTR_S_RECEIPT, ESC
							+ "|4C" + "Double High and Wide\n");// Prints
					// double-high
					// and
					// double-wide
					// characters.
				} catch (JposException e1) {
					e1.printStackTrace();
				}

				try {
					sBuf = String.format("|%dlF", jPos_Printer
							.getRecLinesToPaperCut());
				} catch (JposException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

				try {
					jPos_Printer.printNormal(POSPrinterConst.PTR_S_RECEIPT, ESC
							+ sBuf);
				} catch (JposException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

				try {
					boolean blResult = jPos_Printer.getCapRecPapercut();
					if (blResult == true) {
						switch (nCut) {
						case 0:
							jPos_Printer.cutPaper(50);
							break;
						case 1:
							jPos_Printer.cutPaper(100);
							break;
						case 2:
							break;
						}
					}
				} catch (JposException e3) {
					// TODO Auto-generated catch block
					e3.printStackTrace();
				}

				try {
					jPos_Printer.transactionPrint(
							POSPrinterConst.PTR_S_RECEIPT,
							POSPrinterConst.PTR_TP_NORMAL);
				} catch (JposException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		
		jButton_SamplePrint.setText("Sample Print");
		jPanel_Frame.add(jButton_SamplePrint);
		jPanel_Frame.setLayout(null);
		
		int iLeftBase = 30;
		int iTopBase = 40;
		int iWidthBase = 110;
		int iHeightBase = 30;
		jButton_SamplePrint.setBounds(iLeftBase, iTopBase, iWidthBase,
				iHeightBase);
		
		jButton_SamplePrint.setMargin(Insets_Button);
		
		pack();
	}
	private void InitDialog() {
		boolean bError = false;

		while (true) {
			try {
				jPos_Printer.open("byprn");
			} catch (JposException e) {
				e.printStackTrace();
				JOptionPane.showMessageDialog(null,
						"This device has not been registered, or can't use.",
						"Printer", 0);
				bError = true;
				break;
			}

			try {
				jPos_Printer.claim(1000);
			} catch (JposException e) {
				e.printStackTrace();
				JOptionPane.showMessageDialog(null,
						"Fails to get the exclusive right for the device.",
						"Printer", 0);
				bError = true;
				break;
			}

			try {
				jPos_Printer.setDeviceEnabled(true);
			} catch (JposException e) {
				e.printStackTrace();
				JOptionPane.showMessageDialog(null,
						"Now the device is disabled to use.", "Printer", 0);
				bError = true;
				break;
			}

			try {
				jPos_Printer.setMapMode(POSPrinterConst.PTR_MM_METRIC);
			} catch (JposException e) {
				e.printStackTrace();
			}

			try {
				int iRecLineWidth = jPos_Printer.getRecLineWidth();
				jPos_Printer.setBitmap(1, POSPrinterConst.PTR_S_RECEIPT,
						"bmp\\Lion.bmp", iRecLineWidth / 3,
						POSPrinterConst.PTR_BM_CENTER);
			} catch (JposException e) {
				e.printStackTrace();
				JOptionPane.showMessageDialog(null,
						"Can not find the specific bitmap", "Printer", 0);
			}
			break;
		}
		if (bError == true) {
			jButton_SamplePrint.setEnabled(false);
			return;
		}
		nCut = 0;
	}
	public PrinterTry() {
		InitFram();
		InitDialog();
	}
}


